/**    
 * @Title: ScheduleRunnable.java  
 * @Package cn.com.ytst.data.integration.job.quartz  
 * @Description: TODO(用一句话描述该文件做什么)  
 * @author yanhewei@vv.cc.com    
 * @date 2019年10月8日 下午3:19:22  
 * @version V1.0    
*/
package cn.com.ytst.data.integration.manager.job;

import java.util.concurrent.Callable;

import org.apache.commons.lang3.reflect.MethodUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.ytst.framework.common.StringTools;
import com.ytst.framework.common.tools.DateUtil;

import cn.com.ytst.data.integration.CommonKey;
import cn.com.ytst.data.integration.entity.TaskLog;
import cn.com.ytst.data.integration.util.DictionaryEnu;




/**
 * @ClassName: ScheduleRunnable
 * @Description:
 * @author yanhewei@vv.cc.com
 * @date 2019年10月8日 下午3:19:22
 * 
 */
public class ScheduleRunnable implements Runnable {

	private Logger logger = LoggerFactory.getLogger(ScheduleRunnable.class);
	
	private TaskManager taskmanager;

	private TaskLog tasklog;

	private String prarms;

	private Object object;
	
	ListeningExecutorService listeningExecutorService;
	
	//TaskLogService taskLogService;

	public ScheduleRunnable(String prarms,Object object,TaskManager taskmanager,TaskLog tasklog,ListeningExecutorService listeningExecutorService) {
		this.prarms = prarms;
		this.object = object;
		this.taskmanager = taskmanager;
		this.tasklog = tasklog;
		this.listeningExecutorService = listeningExecutorService;
		//this.taskLogService = taskLogService;
	}
	
	@Override
	public void run() {
		//String serviceClass = taskmanager.serviceClass;
		tasklog.setStart_time(DateUtil.now("yyyy-MM-dd HH:mm:ss"));
		tasklog.setTaskname(taskmanager.jobName);
		String methodString = taskmanager.methodName;

		Object[] p = null;
		if (!StringTools.isBlank(prarms)) {
			p = new Object[] { prarms };
		}
		try {
			Object result = MethodUtils.invokeExactMethod(this.object, methodString, p);
			if (result instanceof String) {
				if (result.toString().equals(DictionaryEnu.SUCCESS.name())) {
					this.tasklog.setEnd_time(DateUtil.now("yyyy-MM-dd HH:mm:ss"));
					this.tasklog.setStatus(CommonKey.SUCCESS_TAG);
					this.tasklog.setFilename(prarms);
					saveLog(this.tasklog);
				} else {
					this.tasklog.setStatus(CommonKey.FAIL_TAG);
					this.tasklog.setEnd_time(DateUtil.now("yyyy-MM-dd HH:mm:ss"));
					this.tasklog.setError_msg(result.toString());
					this.tasklog.setFilename(this.prarms);
					saveLog(this.tasklog);
				}
			}

		} catch (Throwable e) {
			e.printStackTrace();
			this.tasklog.setStatus(CommonKey.FAIL_TAG);
			this.tasklog.setFilename(this.prarms);
			this.tasklog.setEnd_time(DateUtil.now("yyyy-MM-dd HH:mm:ss"));
			this.tasklog.setError_msg(Throwables.getStackTraceAsString(e.fillInStackTrace()));
			saveLog(this.tasklog);
		}

	}

	protected static class LogTask implements Callable<String> {

		private Logger loggerTask = LoggerFactory.getLogger(LogTask.class);

		//private TaskLogService taskLogService;

		private TaskLog tasklog;

		public LogTask(TaskLog tasklog) {
			//this.taskLogService = taskLogService;
			this.tasklog = tasklog;

		}

		@Override
		public String call() throws Exception {
			try {
				int num = 200;
				return num == 200 ? "日志插入成功!" : "日志插入失败!";
			} catch (Exception ex) {
				loggerTask.error("", ex);
				return "系统错误!";
			}

		}

	}

	private void saveLog(TaskLog tasklog) {

		ListenableFuture<String> listenableFuture = listeningExecutorService
				.submit(new LogTask(tasklog));
		Futures.addCallback(listenableFuture, new FutureCallback<String>() {

			@Override
			public void onSuccess(String result) {
				logger.info(result);
			}

			@Override
			public void onFailure(Throwable t) {
				logger.error("{}发生错误,异常原因:", t.getCause());

			}

		});
	}

}
